﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GenesisOne
{
    static class SUpdate
    {
        static public void ToLeft(Character character, GameTime gameTime)
        {
            if (character.Index < character.MaxIndex)
                character.SourceRectangle = new Rectangle(100 * (int)character.Index, 120, 100, 120);

            character.Index += gameTime.ElapsedGameTime.Milliseconds * 0.025f;

            if (character.Index >= 19)
                character.Index = 0;

            character.LastMov = false;

        }

        static public void ToRight(Character character, GameTime gameTime)
        {
            if (character.Index < character.MaxIndex)
                character.SourceRectangle = new Rectangle(100 * (int)character.Index, 0, 100, 120);

            character.Index += gameTime.ElapsedGameTime.Milliseconds * 0.025f;

            if (character.Index >= 19)
                character.Index = 0;

            character.LastMov = true;
        }

        static public void Static(Character character, GameTime gameTime)
        {
            if (character.LastMov == true)
                character.SourceRectangle = new Rectangle(100 * 0, 240, 100, 120);
            else
                character.SourceRectangle = new Rectangle(100 * 1, 240, 100, 120);

            /*
            if (character.Index < character.MaxIndex)
                character.SourceRectangle = new Rectangle(100 * (int)character.Index, 0, 100, 120);

            character.Index += gameTime.ElapsedGameTime.Milliseconds * 0.025f;

            if (character.Index >= 19)
                character.Index = 0;
             */
        }

        static public void Jump(Character character, GameTime gameTime)
        {
            //if (character.Index < character.MaxIndex)
            if (character.LastMov == false)
                character.SourceRectangle = new Rectangle(100 * 6, 120, 100, 120);
            else
                character.SourceRectangle = new Rectangle(100 * 6, 0, 100, 120);

            //character.Index += gameTime.ElapsedGameTime.Milliseconds * 0.025f;

            //if (character.Index >= 19)
            //    character.Index = 0;
        }

        static public void Movement(Character character, GraphicsDeviceManager graphics, Map map, GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                if (character.Jumpt == 0)
                {
                    character.Jumpt = 1;
                    SUpdate.Jump(character, gameTime);
                }

            }

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                character.Translation = new Vector2(character.Translation.X - 1 * character.Speed, character.Translation.Y);
                if (character.Jumpt != 1)
                    SUpdate.ToLeft(character, gameTime);
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                character.Translation = new Vector2(character.Translation.X + 1 * character.Speed, character.Translation.Y);
                if (character.Jumpt != 1)
                    SUpdate.ToRight(character, gameTime);
            }
            else
            {
                character.Index = 0;
                SUpdate.Static(character, gameTime);
            }

            if (character.Position.Y + (character.Texture.Height / 3) < graphics.PreferredBackBufferHeight - 64)
            {
                if (character.LastMov == false)
                    character.SourceRectangle = new Rectangle(100 * 6, 120, 100, 120);
                else
                    character.SourceRectangle = new Rectangle(100 * 6, 0, 100, 120);
            }

            if (character.Position.X < 0)
                character.Translation = new Vector2(1, character.Translation.Y);
            else if (character.Position.X > graphics.PreferredBackBufferWidth - 100)
                character.Translation = new Vector2(-1, character.Translation.Y);

            if (character.Position.Y < 0)
                character.Translation = new Vector2(character.Translation.X, 1);
        }
    }
}
